package leetcode;

import java.util.Stack;

/**
 * Created by 林剑 on 2016/10/7.
 */
public class leetcode32 {
    public int longestValidParentheses(String s) {
    Stack<Integer> stack = new Stack<Integer>();
    int length = s.length();
    boolean[] booleans = new boolean[length];
    int max = 0;
    int cur = 0;
    for(int i=0;i<length;i++){
        if(s.charAt(i) == '('){
            stack.push(i);
        }
        if(s.charAt(i) == ')'&&!stack.isEmpty()){
            booleans[i] = true;
            booleans[stack.pop()]=true;
        }
    }
    for (int i = 0;i<length;i++){
        if(booleans[i] == true) cur++;
        else cur = 0;
        max = max>cur?max:cur;
    }
        return max;
    }

    public static void main(String[] args) {
        leetcode32 leetcode32 = new leetcode32();
        System.out.println(leetcode32.longestValidParentheses("(()"));
    }
}
